package org.dromara.quick.chat.service.impl;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.IdGenerator;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mongo.utils.ObjectDisposal;
import org.dromara.common.mongo.wrapper.WrapperCriteria;
import org.dromara.quick.chat.domain.ImChatConversation;
import org.dromara.quick.chat.model.bo.ImChatConversationReadMaxSeqBO;
import org.dromara.quick.chat.service.ImChatConversationService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 最近会话表实体类
 * @Author: Stars
 * @Date: 2025/5/18 20:41
 */
@Service
@RequiredArgsConstructor
public class ImChatConversationServiceImpl implements ImChatConversationService {

    private final MongoTemplate mongoTemplate;


//    @Lock4j(keys = {"#imChatConversationReadMaxSeqBO.getReceiverId() + '_' + #imChatConversationReadMaxSeqBO.getUserId()()"})
    @Override
    public void updateReadMaxSeq(ImChatConversationReadMaxSeqBO imChatConversationReadMaxSeqBO) {
        ImChatConversation imChatConversation = MapstructUtils.convert(imChatConversationReadMaxSeqBO, ImChatConversation.class);
        imChatConversation.setUpdateTime(new Date());
        WrapperCriteria<ImChatConversation> wrapperCriterie = new WrapperCriteria<>();
        wrapperCriterie.and(ImChatConversation::getReceiverId).is(imChatConversationReadMaxSeqBO.getReceiverId().toString());
        Update update = ObjectDisposal.update(imChatConversation);
        // 仅插入时使用
        update.setOnInsert("id",IdGenerator.getIdStr());
        mongoTemplate.upsert(Query.query(wrapperCriterie),update,ImChatConversation.class);
    }

    @Override
    public void updateDeleteMaxSeq(ImChatConversationReadMaxSeqBO imChatConversationReadMaxSeqBO) {
        ImChatConversation imChatConversation = MapstructUtils.convert(imChatConversationReadMaxSeqBO, ImChatConversation.class);
        imChatConversation.setUpdateTime(new Date());
        WrapperCriteria<ImChatConversation> wrapperCriterie = new WrapperCriteria<>();
        wrapperCriterie.and(ImChatConversation::getReceiverId).is(imChatConversationReadMaxSeqBO.getReceiverId().toString());
        Update update = ObjectDisposal.update(imChatConversation);
        // 仅插入时使用
        update.setOnInsert("id",IdGenerator.getIdStr());
        mongoTemplate.upsert(Query.query(wrapperCriterie),update,ImChatConversation.class);
    }

    @Override
    public ImChatConversation selectByCondition(String senderId, String receiverId, Integer channelType) {
        WrapperCriteria<ImChatConversation> wrapperCriterie = new WrapperCriteria<>();
        wrapperCriterie.and(ImChatConversation::getSenderId).is(senderId)
            .and(ImChatConversation::getReceiverId).is(receiverId)
            .and(ImChatConversation::getChannelType).is(channelType);
        return mongoTemplate.findOne(Query.query(wrapperCriterie), ImChatConversation.class);
    }
}
